<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>demo</title>
</head>
<body>
  <div id="root"></div>
  <script src="../../node_modules/react/umd/react.development.js"></script>
  <script src="../../node_modules/react-dom/umd/react-dom.development.js"></script>
  <script src="./babel.min.js"></script>
  <script type="text/babel">
      const isLogin = false;
    // 登陆子组件
    function Login() {
      return (<div>Already Login!</div>);
    }
    // 非登陆子组件
    function UnLogin() {
      return (<div>Haven't Login!</div>);
    }
    const welcomeStr = "welcome to React";
    const styleObject = {
      color: "blue",
    }

    const users = [{
      name: 'wang', id: 1, age: 17
    },{
      name: 'zhao', id: 2, age: 20
    }, {
      name: 'lai', id: 3, age: 18
    }];
    

    function UserList({count, setCount}) {
        // 列表项
        const userList = users.map((user, index) => {
          return (
            <li
              key={user.id}
              style={{color: user.age < 18 ? 'red': 'green'}}
              onClick={()=> {setCount(count + 1)}}
            >
              {user.name}
            </li>)
        })
        return (
          <div>
            <ul>{userList}</ul>
          </div>
        )
    }
    

    function MyUsers() {
      // useState 是一个我们常说的 Hook
      const [count, setCount] = React.useState(0);
      return (
        <div>
          <p>{count}</p>
          <UserList count={count} setCount={setCount} />
        </div>
      );
    }

    // 组件：组件是页面上的一部分
    function HiButton() {
      let content = <UnLogin />;
      if(isLogin) {
        content =  <Login />
      }
      return (
        <div>
          <button onClick={() => { alert('hi')}}>
            sayHi
          </button>
          <h1 className="test" style={styleObject}>{welcomeStr}</h1>
            { content } 
            <MyUsers/>
        </div>
      )
    }
    // 获取一个页面根节点
    const rootNode = document.getElementById('root');
    // 创建一个 React 根实例
    const root = ReactDOM.createRoot(rootNode);
    // 使用跟实例渲染一个组件
    root.render(React.createElement(HiButton));
  </script>
</body>
</html>